﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Rison.Web.Common;
using Rison.BusinessFacade.Entity.Biz;
using Rison.BusinessFacade.Facade.Biz;
using Rison.BusinessFacade.Common;
using Rison.BusinessFacade.Helper;

namespace Rison.Web.wscp
{
    public partial class CPByGuest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!string.IsNullOrEmpty(labMsg.Text)) labMsg.Text = string.Empty;
            
            if (!this.IsPostBack)
            {
                labMsg.Visible = false;

                BindDepartment();
                Getinformation();

                //labCurItemIndex.Text = "0";
                //labCurDepart.Text = chkDepart.Items[0].Text;
                //this.chkDepart.Items[0].Attributes.CssStyle.Add("color", "red");//默认开始评测第一个部门，置为红色
                //NextCheckbox(0);
                //this.chkDepart.Attributes.CssStyle.Add("border", "1px solid red");
            }
        }

        public void NextCheckbox(int index)
        {
            for (int i = 0; i < chkDepart.Items.Count; i++)
            {
                if (i != index)
                {
                    this.chkDepart.Items[i].Enabled = false;
                }
                else
                {
                    this.chkDepart.Items[i].Enabled = true;
                    this.chkDepart.Items[i].Selected = true;
                    this.labCurItemIndex.Text = i.ToString();
                    this.labCurDepart.Text = this.chkDepart.Items[i].Text;
                    this.chkDepart.Items[i].Attributes.CssStyle.Add("color", "red");
                    this.txtSuggestion.Text = "";
                }
            }
        }

        public void Getinformation()
        {
            CPResultDetailFacade facade = new CPResultDetailFacade();
            DataTable dt = facade.GetWSCPbyPwd(Password).Tables[0];
            int index = 0;
            if (dt.Rows.Count == 0)
            {
                index = 0;
                NextCheckbox(index);
            }
            else
            {

                for (int i = 0; i < chkDepart.Items.Count; i++)
                {
                    if (dt.Select("MailBoxID='" + chkDepart.Items[i].Value + "'").Length != 0)
                    {
                        this.chkDepart.Items[i].Selected = true;
                    }
                    else
                    {
                        index = i;
                        NextCheckbox(index);
                        return;                        
                    }
                }  
            }
            
        }

        public string Password
        {
            get
            {
                if (HttpContext.Current.Session["pwd"] != null)
                    return HttpContext.Current.Session["pwd"].ToString();

                return "";
            }           
        }

        public string IPAddress
        {
            get
            {
                return Page.Request.UserHostAddress.ToString();
            }
        }

        private void BindDepartment()
        {
            //for (int i = 1; i < 64; i++)
            //{
            //    this.chkDepart.Items.Add(new ListItem("部门_" + i.ToString(), "466fbbdf-9d21-4203-9c48-d761e30be2cc"));
            //    this.chkDepart.Items[i - 1].Attributes.CssStyle.Add("padding-left", "10px");
            //}


            CPResultDetailFacade facade = new CPResultDetailFacade();
            DataSet data = facade.GetDepartments();
            this.chkDepart.DataSource = data.Tables[0].DefaultView;
            this.chkDepart.DataValueField = "bmid";
            this.chkDepart.DataTextField = "bmmc";
            this.chkDepart.DataBind();

            data.Dispose(); data = null; facade = null;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Default.aspx");
        }

        private bool ValidateResult()
        {
            if (rblResult_1.SelectedIndex < 0)
                labMsg.Text = "请选择对‘依法行政’的评价！";
            else if (rblResult_2.SelectedIndex < 0)
                labMsg.Text = "请选择对‘行政效能’的评价！";
            else if (rblResult_3.SelectedIndex < 0)
                labMsg.Text = "请选择对‘政务公开’的评价！";
            else if (rblResult_4.SelectedIndex < 0)
                labMsg.Text = "请选择对‘服务态度’的评价！";
            else if (rblResult_5.SelectedIndex < 0)
                labMsg.Text = "请选择对‘清正廉洁’的评价！";

            if (!string.IsNullOrEmpty(labMsg.Text))
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('" + labMsg.Text + "');</script>");

            return string.IsNullOrEmpty(labMsg.Text);
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (!ValidateResult()) return;

            ////对一个部门的评价的答案之间，用逗号间隔
            //StringBuilder result = new StringBuilder();
            //result.Append(rblResult_1.SelectedIndex.ToString());
            //result.AppendFormat(",{0}", rblResult_2.SelectedIndex.ToString());
            //result.AppendFormat(",{0}", rblResult_3.SelectedIndex.ToString());
            //result.AppendFormat(",{0}", rblResult_4.SelectedIndex.ToString());
            //result.AppendFormat(",{0}", rblResult_5.SelectedIndex.ToString());

            ////对不同部门的评价的答案之间，用分号间隔
            //if (hidCheckedResult.Value.Length > 0)
            //    hidCheckedResult.Value += ";";
            //hidCheckedResult.Value += result.ToString(); result = null;

            //this.rblResult_1.SelectedIndex = -1; this.rblResult_2.SelectedIndex = -1;
            //this.rblResult_3.SelectedIndex = -1; this.rblResult_4.SelectedIndex = -1; this.rblResult_5.SelectedIndex = -1;

            int index = Convert.ToInt32(labCurItemIndex.Text);

            SaveNewData();

            if (index + 1 == chkDepart.Items.Count)
            {
                this.labCurItemIndex.Text = (index + 1).ToString();
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('您已完成了对所有部门的评价，感谢您对我们工作的支持！');location.href='../default.aspx';</script>");
            }

            this.rblResult_1.SelectedIndex = -1; this.rblResult_2.SelectedIndex = -1;
            this.rblResult_3.SelectedIndex = -1; this.rblResult_4.SelectedIndex = -1; this.rblResult_5.SelectedIndex = -1;

            NextCheckbox(index+1);
            
            //int index = Convert.ToInt32(labCurItemIndex.Text);
            //if (index == chkDepart.Items.Count - 1)
            //{
            //    //完成了对最后一个部门的评价
            //    this.btnNext.Enabled = false;

            //    SaveData();
            //}
            //else
            //{
            //    this.chkDepart.Items[index].Selected = true;
            //    this.labCurItemIndex.Text = (index + 1).ToString();
            //    this.labCurDepart.Text = this.chkDepart.Items[index + 1].Text;
            //    this.chkDepart.Items[index + 1].Attributes.CssStyle.Add("color", "red");//当前作答的题目，置为红色

            //    for (int i = 0; i < chkDepart.Items.Count; i++)
            //    {
            //        if (i <= index)
            //            this.chkDepart.Items[i].Enabled = false;

            //        //由于会换行，特此请修改
            //        this.chkDepart.Items[i].Attributes.CssStyle.Add("padding-left", "2px");
            //    }
            //}
        }

        //保存评价结果
        private void SaveNewData()
        {
            WSCPResult result = null;

            List<WSCPResult> results = new List<WSCPResult>();
            int i = Convert.ToInt32(labCurItemIndex.Text);
           
            result = new WSCPResult(); results.Add(result);
            result.BMGuid = chkDepart.Items[i].Value; 
            result.BMMC = chkDepart.Items[i].Text;
            result.YFXZ = rblResult_1.SelectedItem.Text;
            result.XZXN = rblResult_2.SelectedItem.Text;
            result.ZWGK = rblResult_3.SelectedItem.Text;
            result.FWTD = rblResult_4.SelectedItem.Text;
            result.QZLJ = rblResult_5.SelectedItem.Text;
            result.Suggestion = txtSuggestion.Text;
            result.FromType = cpType.密码登陆.ToString();
            result.EvaluationUserPassword = Password;
            result.EvaluationUserIP = IPAddress;
 

            CPResultDetailFacade facade = new CPResultDetailFacade();
            DataRow dr = facade.GetWSCPbydepart(Password, result.BMGuid);
            if (dr == null)
            {
                bool success = facade.Save(results, Request.UserHostAddress, true);

                result = null; facade = null;

                if (success)
                {
                    //this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('您已完成了对所有部门的评价，感谢您对我们工作的支持！');location.href='../default.aspx';</script>");
                }
                else
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('对不起，提交失败！可能是因为网络比较繁忙，请稍后再试...');</script>");
            }
        }


        private void SaveData()
        {
            WSCPResult result = null;

            List<WSCPResult> results = new List<WSCPResult>();

            string[] resultVal = hidCheckedResult.Value.Split(Convert.ToChar(";"));

            for (int i = 0; i < chkDepart.Items.Count; i++)
            {
                string[] selectedIndex = resultVal[i].Split(Convert.ToChar(","));

                result = new WSCPResult(); results.Add(result);
                result.BMGuid = chkDepart.Items[i].Value; result.BMMC = chkDepart.Items[i].Text; result.YFXZ = rblResult_1.Items[Convert.ToInt32(selectedIndex[0])].Text;
                result = new WSCPResult(); results.Add(result);
                result.BMGuid = chkDepart.Items[i].Value; result.BMMC = chkDepart.Items[i].Text; result.XZXN = rblResult_2.Items[Convert.ToInt32(selectedIndex[1])].Text;
                result = new WSCPResult(); results.Add(result);
                result.BMGuid = chkDepart.Items[i].Value; result.BMMC = chkDepart.Items[i].Text; result.ZWGK = rblResult_3.Items[Convert.ToInt32(selectedIndex[2])].Text;
                result = new WSCPResult(); results.Add(result);
                result.BMGuid = chkDepart.Items[i].Value; result.BMMC = chkDepart.Items[i].Text; result.FWTD = rblResult_4.Items[Convert.ToInt32(selectedIndex[3])].Text;
                result = new WSCPResult(); results.Add(result);
                result.BMGuid = chkDepart.Items[i].Value; result.BMMC = chkDepart.Items[i].Text; result.QZLJ = rblResult_5.Items[Convert.ToInt32(selectedIndex[4])].Text;
                result.FromType = cpType.密码登陆.ToString();
                result.EvaluationUserPassword = Password;
                result.EvaluationUserIP = IPAddress;
            }

            CPResultDetailFacade facade = new CPResultDetailFacade();
            bool success = facade.Save(results, Request.UserHostAddress, true);

            result = null; facade = null;

            if (success)
            {
                //this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('您已完成了对所有部门的评价，感谢您对我们工作的支持！');location.href='../default.aspx';</script>");
            }
            else
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('对不起，提交失败！可能是因为网络比较繁忙，请稍后再试...');</script>");

        }

        private void SaveData_Original()
        {
            CPResult result = null;
            List<CPResult> results = new List<CPResult>();

            string[] resultVal = hidCheckedResult.Value.Split(Convert.ToChar(";"));

            for (int i = 0; i < chkDepart.Items.Count; i++)
            {
                string[] selectedIndex = resultVal[i].Split(Convert.ToChar(","));

                result = new CPResult(); results.Add(result);
                result.BMGuid = chkDepart.Items[i].Value; result.CPXM = "依法行政"; result.CPJG = rblResult_1.Items[Convert.ToInt32(selectedIndex[0])].Text;
                result = new CPResult(); results.Add(result);
                result.BMGuid = chkDepart.Items[i].Value; result.CPXM = "行政效能"; result.CPJG = rblResult_2.Items[Convert.ToInt32(selectedIndex[1])].Text;
                result = new CPResult(); results.Add(result);
                result.BMGuid = chkDepart.Items[i].Value; result.CPXM = "政务公开"; result.CPJG = rblResult_3.Items[Convert.ToInt32(selectedIndex[2])].Text;
                result = new CPResult(); results.Add(result);
                result.BMGuid = chkDepart.Items[i].Value; result.CPXM = "服务态度"; result.CPJG = rblResult_4.Items[Convert.ToInt32(selectedIndex[3])].Text;
                result = new CPResult(); results.Add(result);
                result.BMGuid = chkDepart.Items[i].Value; result.CPXM = "清正廉洁"; result.CPJG = rblResult_5.Items[Convert.ToInt32(selectedIndex[4])].Text;
            }

            CPResultDetailFacade facade = new CPResultDetailFacade();
            bool success = facade.Save_original(results, null, Request.UserHostAddress);

            results = null; facade = null; result = null; resultVal = null;

            if (success)
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('您已完成了对所有部门的评价，感谢您对我们工作的支持！');location.href='../default.aspx';</script>");
            }
            else
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('对不起，提交失败！可能是因为网络比较繁忙，请稍后再试...');</script>");

        }

    }
}
